package models

import (
	"github.com/beego/beego/v2/client/orm"
	"weiboxs.com/new_h_glasses/utils"
)

//DateManufacture int64  //生产日期
//BatchNumbers    string //批号
//EffectiveDate   int64  //有效日期
type ErpFlowingData struct {
	Id                int64
	DepartmentId      int64
	Typeid            int //类型1采购入库2冲红出库3退货出库4报损出库5盘盈入库6盘亏出库7销售出库8销退入库9调拨出库10调拨入库11调拨退回
	Flowing           *ErpFlowing        `orm:"rel(fk);null;on_delete(do_nothing)"`
	Products          *ErpProducts       `orm:"rel(fk);null;on_delete(do_nothing)"`
	Acceptance        *ErpAcceptanceData `orm:"rel(fk);null;on_delete(do_nothing)"` //验收ID
	Snd               *ErpProductsSnd    `orm:"rel(fk);null;on_delete(do_nothing)"`
	InventoryId       int64                     //入库ID
	Diopter           string `json:"-" orm:"-"` //屈光度
	Astigmatism       string `json:"-" orm:"-"` //散光度
	Addlight          string `json:"-" orm:"-"` //下加光
	ColourNumber      string `json:"-" orm:"-"`
	ModelNumber       string `json:"-" orm:"-"`
	Size              string `json:"-" orm:"-"` //尺寸
	FunctionInfo      string `json:"-" orm:"-"` //功效
	FilmColor         string `json:"-" orm:"-"` //膜层颜色
	Genre             string `json:"-" orm:"-"` //框型
	PrimaryStorageNum int64                     //变动前数量
	StorageNum        int64                     //入库｛变动｝数量
	PurchaseUpNo      string                    //变动单号
	CreatedAt         int64
}

//查询
type ErpFlowingDataQueryParam struct {
	BaseQueryParam
	Filter       string //名称查询
	FlowingId    int64
	ProductName  string `json:"pname"`
	Typeidn      string `json:"typeidn"`
	StartDate    string `json:"start_date"`
	EndDate      string `json:"end_date"`
	BrandName    string `json:"brand_name"`     //品牌名称
	PurchaseUpNo string `json:"purchase_up_no"` //单号
	Hascode      string                         //哈希查询
	Department   int64                          //部门
	Suppliers    string `json:"suppliers"`
	PTypeid      []int //产品类型
	FTypeid      []int //流水类型
	EntryNo      string `json:"entry_no"`
	RelatedSel   []interface{}
}

func (a *ErpFlowingData) TableName() string {
	return ErpFlowingDataTableName()
}

func ErpFlowingDataTableName() string {
	return TableName("erp_flowing_data")
}

func erpFlowingDataPageListQuerySeter(params *ErpFlowingDataQueryParam) orm.QuerySeter {

	query := orm.NewOrm().QueryTable(ErpFlowingDataTableName())
	if len(params.RelatedSel) > 0 {
		query = query.RelatedSel(params.RelatedSel...)
	} else {
		query = query.RelatedSel("Products", "Acceptance", "Snd")
	}

	cond := orm.NewCondition()
	if params.FlowingId > 0 {
		cond = cond.And("flowing_id", params.FlowingId)
	}
	if params.Department > 0 { //按部门查询
		cond = cond.And("department_id", params.Department)
	}

	if len(params.StartDate) > 0 {
		cond = cond.And("CreatedAt__gte", utils.GetDateParse(params.StartDate))
	}
	if len(params.EndDate) > 0 {
		cond = cond.And("CreatedAt__lte", utils.GetDateParse(params.EndDate)+68400)
	}
	if len(params.Typeidn) > 0 {
		cond = cond.And("typeid", params.Typeidn)
	}
	if len(params.PTypeid) > 0 {
		cond = cond.And("Products__TypeId__in", params.PTypeid)
	}
	if len(params.EntryNo) > 0 {
		cond = cond.And("Flowing__PurchaseNo", params.EntryNo)
	}
	if len(params.Suppliers) > 0 { //搜索直接查规格名
		cond = cond.And("Products__Suppliers__Id", params.Suppliers)
	}
	if len(params.ProductName) > 0 { //搜索直接查规格名
		//cond = cond.And("Products__Name__contains", params.ProductName)
		condnn := orm.NewCondition()
		condnb := condnn.Or("Products__BrandName__contains", params.Filter).Or("Products__Name__contains", params.Filter).Or("Products__pinyin__contains", params.Filter).Or("product_name__contains", params.Filter)
		cond = cond.AndCond(condnb)
	}
	if len(params.BrandName) > 0 { //品牌名称搜索
		cond = cond.And("Products__BrandName__contains", params.BrandName)
	}
	if len(params.PurchaseUpNo) > 0 { //单号搜索
		cond = cond.And("PurchaseUpNo__contains", params.PurchaseUpNo)
	}
	query = query.SetCond(cond)

	return query
}

func ErpFlowingDataPageList(params *ErpFlowingDataQueryParam) ([]*ErpFlowingData, int64) {
	ls := make([]*ErpFlowingData, 0)
	query := erpFlowingDataPageListQuerySeter(params)
	total, _ := query.Count()
	query.OrderBy("-Id").Limit(params.Limit, params.Offset).All(&ls)
	return ls, total
}

//按ID获取入库明细
func ErpFlowingDataListForFlowingId(flowing_id int64) []*ErpFlowingData {
	ls := make([]*ErpFlowingData, 0)
	orm.NewOrm().QueryTable(ErpFlowingDataTableName()).Filter("Flowing__id", flowing_id).All(&ls)
	return ls
}
